﻿using Max.PH4_2104A.Shopping.Domain;
using Max.PH4_2104A.Shopping.DTO;
using Max.PH4_2104A.Shopping.IService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Max.PH4_2104A.Shopping.API.Controllers
{
    //[Route("api/[controller]")] //默认路由
    [Route("system/user")] // 自定义路由特性
    [ApiController]
    public class SystemUserController : ControllerBase
    {

        ISystemUserService _service;

        public SystemUserController(ISystemUserService service)
        {
            _service = service;
        }

        /// <summary>
        /// 系统用户登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("login")]
        public IActionResult Login(SystemUserLoginInputDto dto)
        {
            ResponseResultDto response = new();

            var data = _service.Login(dto.UserName, dto.Password);

            response.Code = data.Item1;
            response.Result = data.Item2;

            return Ok(response);
        }

        /// <summary>
        /// 生成随机验证码
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        [HttpGet("code")]
        public IActionResult Code(int p =4)
        {
            ResponseResultDto response = new();
            string code = "";
            code.ToLower(); //转小写
            code.ToUpper();//转大写
            p = p < 4 ? 4 : p;
            #region //第一种：纯数
            //switch (p)
            //{
            //    case 5:
            //        code = new Random().Next(10000, 99999).ToString();
            //        break;
            //    case 6:
            //        code = new Random().Next(100000, 999999).ToString();
            //        break;
            //    default:
            //        code = new Random().Next(1000, 9999).ToString();
            //        break;
            //}
            #endregion

            #region //第二种：数字+字母
            string r = "0123456789abcdefjhijgklmopqrstuvwxyzABCDEFJGKLMOPQRSTUVWXYZ";
            char[] rs = r.ToArray(); //将随机源字符串，转换为字符数组
            int len = rs.Length; //字符数组的长度

            for (int i = 0; i < p; i++)
            {
                var a = new Random().Next(0, len);//随便抽取字符数组中的一个下标

                code += rs[a].ToString();
            }
            #endregion

            response.Result = code;
            return Ok(response);
        }


        [HttpGet("snow")]
        public IActionResult TestSnow()
        {
            /*
             * 1、安装包
             * 2、使用
             * **/

            var id = new Snowflake.Core.IdWorker(1, 1).NextId();


            return Ok(id);
        }
    }
}
